import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'
export default createStore({
	state: {
		token: '',
		refToken: '',
		userName: '',
		paw: '',
		permiJson: '',
		cachedViews: [] as any[],
	},
	mutations: {
		//2、定义常用方法
		addView(state, view) {
			if (state.cachedViews.includes(view.name)) {
				return;
			}
			state.cachedViews.push(view.name)
		},
		deleteView(state, view) {
			const index = state.cachedViews.indexOf(view.name)
			if (index > -1 && state.cachedViews) {
				state.cachedViews.splice(index, 1)
			}
		},
		deleteViewName(state, viewName) {
			const index = state.cachedViews.indexOf(viewName)
			if (index > -1 && state.cachedViews) {
				state.cachedViews.splice(index, 1)
			}
		},
		clearView(state) {
			state.cachedViews = [];
		},
		updateToken(state, token) {
			state.token = token;
		},
		updateRefToken(state, refToken) {
			state.refToken = refToken;
		},
		clearnToken(state) {
			state.refToken = '';
			state.token = '';
			state.userName = '';
			localStorage.clear();
		},
		clearnLocalStorage(state) {
			state.refToken = '';
			state.token = '';
			state.userName = '';
			state.permiJson = '';
			localStorage.clear();
		},
		updateUsername(state, userName) {
			state.userName = userName;
		},
		updatePaw(state, paw) {
			state.paw = paw;
		},
		clearnUser(state) {
			state.userName = '';
			state.paw = '';
		},
		updatePermiJson(state, permiJson) {
			state.permiJson = permiJson;
		},
	},
	actions: {
	},
	modules: {
	},
	plugins: [createPersistedState()]
})